#include "stdafx.h"
#include "TestHash.h"
#include "Hash.h"
#include "Tree.h"
#include "PDMSPerObjManager.h"
#include "TreeTest.h"

using namespace PDMS::PDMSFrame;

CPPUNIT_TEST_SUITE_REGISTRATION( TestHash );


TestHash::TestHash()
{
}


TestHash::~TestHash()
{
}

void TestHash::TestAdd()
{
	PDMSPerObjManager manager;
	PDMS::PDMSFrame::Hash* pHash = manager.HashModel();
	PDMS::PDMSFrame::Tree *pTree = NULL;

	pTree = new TreeTest;
	pTree->Name( "Test" );

	UESoft::ErrorStatus es = pHash->Add( pTree );

	CPPUNIT_ASSERT( pHash->Size() == 1 );
}

void TestHash::TestGetTree()
{
	UESoft::ErrorStatus es = UESoft::eOk;
	PDMSPerObjManager manager;
	PDMS::PDMSFrame::Hash* pHash = manager.HashModel();
	PDMS::PDMSFrame::Tree *pTree = NULL;
	
	CPPUNIT_ASSERT( pHash->Size() == 0 );

	pTree = new TreeTest;
	pTree->Name( "Test" );		
	es = pHash->Add( pTree );
	CPPUNIT_ASSERT( pTree );
	CPPUNIT_ASSERT( pHash->Size() == 1 );

	PDMS::PDMSFrame::Tree* ptmpTree = NULL;

	es = pHash->GetTree( pTree->Id(), ptmpTree );
	CPPUNIT_ASSERT( es == UESoft::eWasOpenForWrite );
	CPPUNIT_ASSERT( ptmpTree == NULL );

	UeObjectId id = pTree->Id();
	es = pTree->Close();
	CPPUNIT_ASSERT( es == UESoft::eOk );

	es = pHash->GetTree( id, ptmpTree );
	CPPUNIT_ASSERT( es == UESoft::eOk );
	CPPUNIT_ASSERT( pTree == ptmpTree );
	ptmpTree->Close();
}

void TestHash::TestRemoveAll()
{
	PDMSPerObjManager manager;
	PDMS::PDMSFrame::Hash* pHash = manager.HashModel();
	PDMS::PDMSFrame::Tree *pTree = NULL;
	
	CPPUNIT_ASSERT( pHash->Size() == 0 );
	
	pTree = new TreeTest;
	pTree->Name( "Test" );	
	UESoft::ErrorStatus es = pHash->Add( pTree );	
	CPPUNIT_ASSERT( es == UESoft::eOk );
	CPPUNIT_ASSERT( pHash->Size() == 1 );
	
	pHash->RemoveAll();
	CPPUNIT_ASSERT( pHash->Size() == 0 );
}

void TestHash::TestDatabase()
{
	PDMSPerObjManager manager;
	
	CPPUNIT_ASSERT( manager.HashModel()->Database() != NULL );
}